/**
 * @Author: Wang chunsheng  email:2192138785@qq.com
 * @Date:   2021-10-30 10:40:38
 * @Last Modified by:   Wang chunsheng  email:2192138785@qq.com
 * @Last Modified time: 2022-08-01 16:18:30
 */
 import request from '@/utils/request'
 import Layout from '@/layout'
 import diandiAdmin from 'diandi-admin'
 // 其他子页面中间件
 
 export function getMenus(token) {
   return request({
     url: '/system/index/menus',
     method: 'get'
   })
 }
 
 export function setBlocCache(data) {
   return request({
     url: 'system/settings/set-cache',
     method: 'post',
     data
   })
 }


export function clearCache(data) {
  return request({
    url: 'system/settings/clear-cache',
    method: 'post',
    data
  })
}
 
 export function getCitylist() {
   return request({
     url: '/map/citylist',
     method: 'get'
   })
 }
 
 export function loadFile(item) {
   if(item.type === 'system'){
     return  diandiAdmin[item.name]
   }else{
      if(item.path === '/main/index.vue'){
        return  diandiAdmin[item.name]
      }else{
        return (resolve) => require(['@/views' + item.path], resolve)        
      }
   }
 }
 
 export function initMenus(menus) {
   menus.forEach((item, index) => {
     // 如果是第一级别的菜单 并且子菜单不为空，name给一级菜单设置全局布局
     switch (item.level_type) {
       case 1:
         // 一级菜单
         item.component = Layout
         if (item.children.length === 1) {
           item.redirect = item.children[0].path
           delete item.name
           // console.log('一2级菜单2 ', item.children)
         }
         // console.log('一2级菜单2 ', item.name, item)
         break
       case 2:
         // 一级菜单无二级
         item.component = Layout // loadFile(item.path)
         break
       case 3:
         // 二级菜单
         item.component = loadFile(item)
         break
       case 4:
         // 二级菜单无三级
         item.component = loadFile(item)
         break
       case 5:
         // 三级菜单,三级菜单下不能包含子页面菜单，只能包含非页面菜单
         item.component = loadFile(item)
         break
       case 6:
         // 非菜单页面
         item.component = loadFile(item)
         break
       default:
         // 默认一级菜单
         item.component = Layout
         break
     }
 
     if (item.children.length > 0) {
       initMenus(item.children)
     }
 
     return item
   })
 
   return menus
 }
 
 export function initLeftMenus(menus) {
   const leftMenu = []
   menus.forEach((item, index) => {
     // 如果是第一级别的菜单 并且子菜单不为空，name给一级菜单设置全局布局
     if (item.parent === 0) {
       leftMenu.push(item)
     }
   })
   return leftMenu
 }
 
 // 初始化菜单
 export function getMenuRoutes(menus) {
  console.log('初始化菜单',menus);
   const menuRoutes = initMenus(menus)
   buildMenuRoutesStep2(menuRoutes)
   return menuRoutes
 }
 
 // 处理非菜单页面，提升一个父子节点
 /**
  * 
  * @param {当前数据} routes 
  * @param {非菜单所在集合} root 
  * @returns 
  */
 function buildMenuRoutesStep2(routes, root) {
   let list = []
   return routes.filter(its => {
     if (its.level_type === 6) {
       // 过滤掉非页面菜单
       root.children = root.children.filter(chi => chi.id === its.id)
     } else {
       root = its
       // 过滤孙子辈
       list = its.children.filter(son => {
        if (son.children.length>0) {
          const sonl = son.children.filter(sonchi => sonchi.level_type === 6)
          if (sonl.length > 0) {
            return sonl
          } else {
            return false
          }
        } else {
          return false
        }
      })
      //  将非菜单页面提升一个父子集节点
       if (list.length > 0) {
        upRoute(list,its)
       }
     }
     
    if(root.children.length>0){ 
      console.log('its.children',its.children, root);
      return buildMenuRoutesStep2(its.children, root)      
     }
   })
 }

 function upRoute(list,its){
  list.forEach((l, index) => {
    its.children = its.children.concat(l.children)
    l.children = []
  })
 }